<?php

/**
 * Contains a class to define a Event object to describe an Event's properties.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */

/** @uses encode For strict HTML compliant output. */
require_once 'function_encode.php';
 
/**
 * A class to define a Event object to describe an Event's properties.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class Event
{
    /**
     * The database unique identifier for this event.
     *
     * @access public
     * @var int       Corresponds to events.eventid
     */
    public $id;
    
    /**
     * The name of the event (e.g., Bob's Hot Dog Shack).
     *
     * @access public
     * @var string    Corresponds to events.name
     */
    public $name;
    
    /**
     * The description of the event.
     *
     * @access public
     * @var string    Corresponds to events.description
     */
    public $description;
    
    /**
     * The UNIX timestamp of the date that the event was added.
     *
     * @access public
     * @var int       Corresponds to events.dateadded
     */
    public $dateAdded;
    
    /**
     * The UNIX timestamp of the date that the event starts.
     *
     * @access public
     * @var int       Corresponds to events.startime
     */
    public $startTime;

    /**
     * The UNIX timestamp of the date that the event ends.
     *
     * @access public
     * @var int       Corresponds to events.endtime
     */
    public $endTime;
    
    /**
     * The street address of the event.
     *
     * @access public
     * @var string    Corresponds to events.address
     */
    public $address;
    
    /**
     * The city in which the event is located.
     *
     * @access public
     * @var string    Corresponds to events.city
     */
    public $city;
    
    /**
     * The state in which the event is located.
     *
     * @access public
     * @var string    Corresponds to events.state
     */
    public $state;
    
    /**
     * The ZIP code in which the event is located.
     *
     * @access public
     * @var string    Corresponds to events.zip
     */
    public $zip;
    
    /**
     * The country in which the event is located.
     *
     * @access public
     * @var string    Corresponds to events.country
     */
    public $country;
    
    /**
     * The latitude of the event's geographic location.
     *
     * @access public
     * @var float     Corresponds to events.latitude
     */
    public $latitude;
    
    /**
     * The longitude of the event's geographic location.
     *
     * @access public
     * @var float     Corresponds to events.longitude
     */
    public $longitude;
    
    /**
     * The path to the image for this event, or to the null image.
     *
     * @access public
     * @var string    Corresponds to eventimages
     */
    public $imagePath;

    /**
     * The distance between the event and the user's home zip code.
     *
     * @access public
     * @var float
     */
    public $distance;

    /**
     * Array of stores connected to event,
     *
     * @access public
     * @var array
     */
    public $stores;
    
    /**
     * Constructor function.  Populates variables based on parameters.
     *
     * @param int    $id          The unique ID for the event.
     * @param string $name        The name of the event.
     * @param string $description The description of the event.
     * @param int    $dateAdded   The date and time the event was added.
     * @param int    $startTime   The date and time the event starts.
     * @param int    $endTime     The date and time the event ends.
     * @param string $address     The street address of the event.
     * @param string $city        The city the event is located in.
     * @param string $state       The state the event is located in.
     * @param string $zip         The zip code of the event.
     * @param string $country     The country the event is located in.
     * @param float  $latitude    The event's latitude.
     * @param float  $longitude   The event's longitude.
     * @param string $imagePath   The path to the event's image.
     * @param float  $distance    The distance between event and user's ZIP.
     * @param array  $stores      Array of all stores connected to event.
     *
     * @access public
     * @return null
     */
    public function __construct($id = null, $name = null, $description = null,
        $dateAdded = null, $startTime = null, $endTime = null, $address = null,
        $city = null, $state = null, $zip = null, $country = null,
        $latitude = null, $longitude = null, $imagePath = null,
        $distance = null, $stores = array()
    ) {
        $this->id          = $id;
        $this->name        = $name;
        $this->description = $description;
        $this->dateAdded   = $dateAdded;
        $this->startTime   = $startTime;
        $this->endTime     = $endTime;
        $this->address     = $address;
        $this->city        = $city;
        $this->state       = $state;
        $this->zip         = $zip;
        $this->country     = $country;
        $this->latitude    = $latitude;
        $this->longitude   = $longitude;
        $this->imagePath   = $imagePath;
        $this->distance    = $distance;
        $this->stores      = $stores;
    }

    /**
     * The clone function. Is called via the PHP5 'clone' keyword.
     * Since the 'clone' keyword automatically does a shallow copy of variables,
     * this code finishes the work and does a deep copy.
     *
     * @access public
     * @return null
     */
    public function __clone()
    {
        foreach ($this as $key => $val) {
            if (is_object($val) || is_array($val)) {
                $this->{$key} = unserialize(serialize($val));
            }
        }
    }
    
    /**
     * Function to print a event as a list item for display on the home page
     * and in store search results.
     *
     * @param bool $userIsOwner whether or not to print the Edit link.
     * @param bool $printAll    Whether or not to print all of the store info.
     *
     * @access public
     * @return null
     */
    public function printListItem($userIsOwner = false, $printAll = false)
    {
        echo '  <li>', "\n",
            '   <img src="', $this->imagePath, '" alt="Picture of ',
            encode($this->name), '" />', "\n",
            '   <div>', "\n",
            '    <h2>';
        
        if (!$printAll) {
            echo '<a href="', RELATIVE_PATH, 'event.php?eventid=',
            $this->id, '">', encode($this->name), '</a>';
        } else {
            echo encode($this->name);
        }
        echo '</h2>', "\n",
            '    <address>', "\n",
            '     ', encode($this->address), '<br/>', "\n",
            '     ', encode($this->city), ', ', encode($this->state), ' ',
            $this->zip->zip, '<br/>', "\n",
            '     ', encode($this->country), "\n",
            '    </address>', "\n",
            '    <p>', substr($this->description, 0, 500), '</p>', "\n",
            '    <p><strong>Starts:</strong> ',
            strftime('%D %I:%M%p', $this->startTime), '</p>', "\n",
            '    <p><strong>Ends:</strong> ',
            strftime('%D %I:%M%p', $this->endTime), '</p>', "\n";
        
        if ($this->distance != null) {
            echo '    <p><strong>Distance:</strong> ', 
                number_format($this->distance, 2), ' ', D_DISTANCE, '</p>',
            "\n";
        }
        if ($printAll) {
            $this->_printStores();
        }
        if ($userIsOwner) {
            echo '    <br/><a href="', RELATIVE_PATH,
                'user/edit_event.php?eventid=', $this->id,
                '">Edit Event</a>', "\n",
                '   - <a href="', RELATIVE_PATH, 'admin/events.php?action=delete',
                '&eventid=', $this->id, '" onclick="return confirm(\'Are you sure',
                ' you wish to delete this event?\');">Delete Event</a>', "\n";
        }
        echo '   </div>', "\n",
            '  </li>', "\n";
    }

    /**
     * Function to print a list of stores for this event.
     *
     * @access private
     * @return null
     */
    private function _printStores()
    {
        if (count($this->stores) == 0) {
            return null;
        }
        echo '    <p><strong>Stores:</strong></p>', "\n",
            '    <ul>', "\n";
        foreach ($this->stores as $store) {
            echo '     <li><a href="', RELATIVE_PATH, 'stores.php?storeid=',
                $store->id, '"><strong>', $store->name, '</strong></a></li>',
                "\n";
        }
        echo '    </ul>', "\n";
    }
    
    /**
     * Function to print the list item with all information present.
     *
     * @param bool $userIsOwner whether or not to print the Edit link.
     *
     * @access public
     * @return null
     */
    public function printListItemFull($userIsOwner = false)
    {
        $this->printListItem($userIsOwner, true);
    }
}

?>